package com.m7788.screen;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.tool.utils.StrUtils;

/**
 * 
 * 
 * 类名称：MyReceiver 类描述： 推送的接收类 创建人： huanghaishui 创建时间：2015-11-5 上午8:49:25
 * 修改人：huanghaishui 修改时间：2015-11-5 上午8:49:25 修改备注：
 * 
 * @version
 * 
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";
	private String key = "", title, content;
	private Context context;
	private NotificationManager notificationManager;

	@Override
	public void onReceive(Context context, Intent intent) {
		this.context = context;
		Bundle bundle = intent.getExtras();
		Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction()
				+ ", extras: " + printBundle(bundle));

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle
					.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) {
			Log.d(TAG,
					"[MyReceiver] 接收到自定义"
							+ bundle.getString(JPushInterface.EXTRA_MESSAGE));
			
//			try {
//
//				String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//				if (StrUtils.isNotEmpty(extra)) {
//
//					String ret = new JSONObject(extra).getString("ret");
//
//				}
//				title = bundle.getString(JPushInterface.EXTRA_TITLE);
//				content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//				showNotification(title,content);
//			} catch (JSONException e) {
//				e.printStackTrace();
//			}

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) {
			Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
			int notifactionId = bundle
					.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			String url = "";
			if(StrUtils.isNotEmpty(extra)){
				try {
					url = new JSONObject(extra).getString("url");
					if(StrUtils.isNotEmpty(url)){
						String alert = bundle.getString(JPushInterface.EXTRA_ALERT);
						AppUtil.sendMesToActivity(context, 209, alert+"##"+url+"##"+notifactionId);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) {
			Log.d(TAG, "[MyReceiver] 用户点击打开了");

			JPushInterface.reportNotificationOpened(context,
					bundle.getString(JPushInterface.EXTRA_MSG_ID));
			
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			
			String url = "";
			if(StrUtils.isNotEmpty(extra)){
				try {
					url = new JSONObject(extra).getString("url");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			Log.e("extraUrl", url);
			//如果当前已经打开了indexscreen，则直接传url 跳转
			if("true".equals(AppUtil.getRecord(context, Constant.ISON)) &&
					StrUtils.isNotEmpty(url)){
				AppUtil.sendMesToActivity(context, 208, url);
			}else{
				//休眠则 打开自定义的Activity
				Intent i = new Intent(context, IndexScreen.class);
				i.putExtra("bundleUrl", url);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(i);
			}
		
		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent
				.getAction())) {
			Log.d(TAG,
					"[MyReceiver] 用户收到到RICH PUSH CALLBACK: "
							+ bundle.getString(JPushInterface.EXTRA_EXTRA));
		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent
				.getAction())) {
		} else {
			Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
		}
	}

	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
			} else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}

//	public void showNotification(String nfTitle, String result) {
//
//		if(StrUtils.isEmpty(nfTitle)){nfTitle = context.getResources().getString(R.string.app_name);}
//		// 创建 NotificationManager，对象负责“发出”与“取消” Notification
//		notificationManager = (NotificationManager) context
//				.getSystemService(Context.NOTIFICATION_SERVICE);
//		Notification notification = new Notification(R.drawable.icon, nfTitle,
//				System.currentTimeMillis());
//
//		notification.flags = Notification.FLAG_AUTO_CANCEL;
//		notification.defaults |= Notification.DEFAULT_VIBRATE;
//		long[] vibrate = { 0, 100, 200, 300 };
//		notification.vibrate = vibrate;
//		Intent nfIntent = new Intent();
//		nfIntent = new Intent(context, IndexScreen.class);
//		nfIntent.putExtra("bundleUrl", "");
//		nfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//		int temp = new Date().getSeconds();
//		PendingIntent contentIntent = PendingIntent.getActivity(context, temp,
//				nfIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//
//		notification
//				.setLatestEventInfo(context, nfTitle, result, contentIntent);
//		notificationManager.notify(temp, notification);
//	}
	
	
//	private void sendNotification(){
//		   NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//		      //设置当有消息是的提示，图标和提示文字
//		      builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
//		      //需要样式
//		      BigTextStyle style = new BigTextStyle();
//		      style.setBigContentTitle("上课通知");//通知的标题
//		      style.bigText("今天下午要在综B上jsp");//通知的文本内容
//		      //大视图文本具体内容
//		      style.setSummaryText("这是正常的课程安排，请各位同学按时上课");
//		      builder.setStyle(style);
//		      //显示消息到达的时间，这里设置当前时间
//		      builder.setWhen(System.currentTimeMillis());
//		      //获取一个通知对象
//		      Notification notification = builder.build();
//		      notification.flags = Notification.FLAG_AUTO_CANCEL;
//		      //发送(显示)通知
//		      //notify()第一个参数id An identifier for this notification unique within your application
//		      //get?意思说，这个通知在你的应用程序中唯一的标识符
//		      NotificationManager manager = (NotificationManager) context
//						.getSystemService(Context.NOTIFICATION_SERVICE);
//		      manager.notify(1, notification);
//	}

}
